﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileManager.cs" company="HelloWorld">
//   BY苏宣
// </copyright>
// <summary>
//   Defines the FileManager type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Service
{
    using System.Collections.Generic;
    using System.IO;

    using WindowsFormsApp2.Model;

    /// <summary>
    /// The file manager.
    /// </summary>
    public class FileManager
    {
        /// <summary>
        /// The base path.
        /// </summary>
        private const string BasePath = @"./myresource";
        
        /// <summary>
        /// The folders.
        /// </summary>
        // ReSharper disable once CollectionNeverQueried.Local
        private readonly List<Folder> folders;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileManager"/> class.
        /// </summary>
        public FileManager()
        {
            // 初始化目录
            Directory.CreateDirectory(BasePath);

            // 遍历子文件夹
            var dir1 = new DirectoryInfo(BasePath);
            var dir2 = dir1.GetDirectories();
            this.Dirs = new List<string>();
            foreach (var d in dir2)
            {
                this.Dirs.Add(d.Name);
            }

            // 初始化管理类
            this.folders = new List<Folder>();
            foreach (var s in this.Dirs)
            {
                var f = new Folder(s);
                f.Init(BasePath + "/" + s);
                this.folders.Add(f);
            }
        }

        /// <summary>
        /// Gets or sets the Directions.
        /// </summary>
        public List<string> Dirs { get; set; }
        
        /// <summary>
        /// The create direction.
        /// </summary>
        /// <param name="dirname">
        /// The direction name.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool CreateDir(string dirname)
        {
            if (this.Dirs.Contains(dirname))
            {
                return false; // 重名文件夹
            }

            var path = BasePath + @"/" + dirname;
            Directory.CreateDirectory(path);
            Directory.CreateDirectory(path + @"/公共");
            Directory.CreateDirectory(path + @"/笔记");
            Directory.CreateDirectory(path + @"/资源");
            this.Dirs.Add(dirname);
            var f = new Folder(dirname);
            f.Init(BasePath + @"/" + dirname);
            this.folders.Add(f);

            return true;
        }
    }
}
